<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="院系名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入院系名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="院系类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择院系类型" clearable>
          <el-option v-for="item in types" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="院系状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择院系状态" clearable>
          <el-option v-for="item in options" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['system:college:list']" type="primary" icon="el-icon-search" size="small"
          @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button type="info" plain icon="el-icon-sort" size="small" @click="toggleExpandAll">展开/折叠</el-button>
        <el-button v-hasPermi="['system:college:add']" type="primary" size="small" plain icon="el-icon-plus"
          @click="handleAdd">新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-if="refreshTable" v-loading="loading" :data="collegeList" row-key="id" :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" align="center" label="院系名称" />
      <el-table-column prop="type" align="center" label="院系类型" />
      <el-table-column label="院系状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="启用" inactive-value="禁用"
            @change="updateCollegeStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['system:college:update']" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-hasPermi="['system:college:add']" v-if="scope.row.type!='班级'" size="mini" type="text"
            icon="el-icon-plus" @click="handleAdd(scope.row)">新增</el-button>
          <el-button v-hasPermi="['system:college:delete']" v-if="scope.row.type != '学院'" size="mini" type="text"
            icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :span="24">
          <el-form-item label="院系类型" prop="type">
            <el-select style="width: 100%;" v-model="form.type">
              <el-option value="分院">分院</el-option>
              <el-option value="专业">专业</el-option>
              <el-option value="班级">班级</el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="24" v-if="form.type !== '学院'">
            <el-form-item label="上级学院" prop="parentId">
              <treeselect v-model="form.parentId" :options="collegeOptions" :normalizer="normalizer"
                placeholder="选择上级学院" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="院系名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入院系名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCollege,
    getCollege,
    delCollege,
    addCollege,
    updateCollege
  } from "@/api/system/college";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "College",
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 表格树数据
        collegeList: [],
        types: ['学院', '分院', '专业', '班级'],
        options: ["启用", "禁用"],
        // 部门树选项
        collegeOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部展开
        isExpandAll: true,
        // 重新渲染表格状态
        refreshTable: true,
        // 查询参数
        queryParams: {
          name: undefined,
          type: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          parentId: [{
            required: true,
            message: "上级学院不能为空",
            trigger: "blur"
          }],
          name: [{
            required: true,
            message: "学院名称不能为空",
            trigger: "blur"
          }],
          type: [{
            required: true,
            message: "学院类型不能为空",
            trigger: "change"
          }]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      updateCollegeStatus(row) {
        let text = row.status
        this.$confirm('确认要"' + text + '""' + row.name + '"岗位吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateCollege(row).then(response => {
            this.$message.success(text + "成功");
            this.getList();
          }).catch(() => {
            this.getList();
          });
        }).catch(() => {
          this.getList();
        });
      },
      getList() {
        this.loading = true;
        listCollege(this.queryParams).then(response => {
          this.collegeList = response.data;
          this.loading = false;
        });
      },
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {}
        this.collegeOptions = []
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs.queryForm.resetFields();
        this.handleQuery();
      },
      getCollegeType() {
        listCollege(this.queryParams).then(response => {
          this.collegeOptions = response.data
        });
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.form.parentId = row.id || this.form.parentId
        this.getCollegeType();
        this.title = "添加院系";
        this.open = true;
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        getCollege(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改院系";
        });
        listCollege().then(response => {
          this.collegeOptions = response.data
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateCollege(this.form).then(response => {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCollege(this.form).then(response => {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除名称为"' + row.name + '"的数据项？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delCollege(row.id).then(() => {
            this.getList();
            this.$message.success("删除成功");
          })
        })
      }
    }
  };
</script>
